package com.asha.md360player4android.vr.objects;

import android.graphics.RectF;

import com.asha.vrlib.MDVRLibrary;

/**
 * Created by android on 7/6/17.
 */

public class PlaneScaleCalculator {
    private static final float sBaseValue = 1.0f;

    private RectF mTextureSize;

    private float mViewportRatio;

    private int mScaleType;

    private float mViewportWidth = sBaseValue;

    private float mViewportHeight = sBaseValue;

    private float mTextureWidth = sBaseValue;

    private float mTextureHeight = sBaseValue;

    public PlaneScaleCalculator(int scaleType, RectF textureSize) {
        this.mScaleType = scaleType;
        this.mTextureSize = textureSize;
    }

    public float getTextureRatio(){
        return mTextureSize.width() / mTextureSize.height();
    }

    public void setViewportRatio(float viewportRatio){
        this.mViewportRatio = viewportRatio;
    }

    public void calculate(){
        float viewportRatio = mViewportRatio;
        float textureRatio = getTextureRatio();

        switch (this.mScaleType){
            case MDVRLibrary.PROJECTION_MODE_PLANE_FULL:
                // fullscreen
                mViewportWidth = mViewportHeight = mTextureWidth = mTextureHeight = sBaseValue;
                break;
            case MDVRLibrary.PROJECTION_MODE_PLANE_CROP:
                if (textureRatio  > viewportRatio){
                    /**
                     * crop width of texture
                     *
                     * texture
                     * ----------------------
                     * |    |          |    |
                     * |    | viewport |    |
                     * |    |          |    |
                     * ----------------------
                     * */
                    mViewportWidth = sBaseValue * viewportRatio;
                    mViewportHeight = sBaseValue;

                    mTextureWidth = sBaseValue * textureRatio;
                    mTextureHeight = sBaseValue;
                } else {
                    /**
                     * crop height of texture
                     *
                     * texture
                     * -----------------------
                     * |---------------------|
                     * |                     |
                     * |      viewport       |
                     * |                     |
                     * |---------------------|
                     * -----------------------
                     * */
                    mViewportWidth = sBaseValue;
                    mViewportHeight = sBaseValue / viewportRatio;

                    mTextureWidth = sBaseValue;
                    mTextureHeight = sBaseValue / textureRatio;
                }
                break;
            case MDVRLibrary.PROJECTION_MODE_PLANE_FIT:
            default:
                if (viewportRatio > textureRatio){
                    /**
                     * fit height of viewport
                     *
                     * viewport
                     * ---------------------
                     * |    |         |    |
                     * |    | texture |    |
                     * |    |         |    |
                     * ---------------------
                     * */
                    mViewportWidth = sBaseValue * viewportRatio ;
                    mViewportHeight = sBaseValue;

                    mTextureWidth = sBaseValue * textureRatio;
                    mTextureHeight = sBaseValue;
                } else {
                    /**
                     * fit width of viewport
                     *
                     * viewport
                     * -----------------------
                     * |---------------------|
                     * |                     |
                     * |       texture       |
                     * |                     |
                     * |---------------------|
                     * -----------------------
                     * */
                    mViewportWidth = sBaseValue;
                    mViewportHeight = sBaseValue / viewportRatio;

                    mTextureWidth = sBaseValue;
                    mTextureHeight = sBaseValue / textureRatio;
                }
                break;
        }
    }

    public float getViewportWidth(){
        return mViewportWidth;
    }

    public float getViewportHeight(){
        return mViewportHeight;
    }

    public float getTextureWidth(){
        return mTextureWidth;
    }

    public float getTextureHeight(){
        return mTextureHeight;
    }
}
